package com.wd.jetpack.binding

import android.graphics.Color
import android.text.TextUtils
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import com.wd.base.util.HandlerUtils
import com.wd.common.glide.LbGlide
import com.wd.jetpack.R

/**
 * 自定义BindingAdapter加载网络图片<BR>
 *
 * @author w4425
 * @version [V5.9.3, 2021/6/27]
 * @since V5.9.3
 */
object ImageBinds {
    /**
     * 加载网络图片
     * @JvmStatic 静态 否则报错
     * If you don't use an inflation method taking a DataBindingComponent, use DataBindingUtil.setDefaultComponent or make all BindingAdapter methods static.
     */
    @BindingAdapter("imageUrl")
    @JvmStatic
    fun bindImage(imgView: ImageView, url: String?) {
        if (imgView == null) {
            return
        }
        HandlerUtils.postDelay(Runnable {
            if (!TextUtils.isEmpty(url)) {
                LbGlide.with(imgView.context)
                        .loading(R.mipmap.ic_launcher)
                        .load(url)
                        .into(imgView)
            } else {
                imgView.setBackgroundColor(Color.GRAY)
            }
        }, 2000)

    }

    @BindingAdapter("resId")
    @JvmStatic
    fun bindImage(imgView: ImageView, resId: Int?) {
        if (imgView == null) {
            return
        }
        if (resId != null) {
            imgView.setImageResource(resId)
        }
    }
}